package com.yanqu.road.server.logic.battle;

import com.yanqu.road.entity.config.battle.BattleResult;
import com.yanqu.road.entity.config.battle.PatronsBattleDetail;
import com.yanqu.road.entity.player.UserPatrons;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class BattleManager {

	private static Logger logger = LogManager.getLogger(BattleManager.class.getName());

	/**
	 * 战斗 全部走这边
	 */
	public static BattleResult battle(long userId, List<UserPatrons> patronsList, long enemyUserId, List<UserPatrons> enemyPatronsList,
		int buffAddition) {
		BattleResult battleResult = new BattleResult();
		List<UserPatrons> clonePatronsList = new ArrayList<>();
		List<Integer> patronsIdList = new ArrayList<>();
		for (UserPatrons patrons : patronsList) {
			patronsIdList.add(patrons.getPatronsId());
			clonePatronsList.add(patrons.cloneUserPatrons());
		}

		List<UserPatrons> cloneEnemyPatronsList = new ArrayList<>();
		for (UserPatrons patrons : enemyPatronsList) {
			cloneEnemyPatronsList.add(patrons.cloneUserPatrons());
		}

		for (UserPatrons userPatrons : clonePatronsList) {
			userPatrons.setCurrentBlood(userPatrons.getAbility());
			BigInteger oldBlood = userPatrons.getCurrentBlood();
			userPatrons.setCurrentBlood(new BigDecimal(oldBlood).multiply(BigDecimal.valueOf(((double)userPatrons.getOreWarBattleAddition() + buffAddition) / 1000)).
					setScale(0, BigDecimal.ROUND_UP).toBigInteger());
			userPatrons.setTotalBlood(userPatrons.getCurrentBlood());
		}

		for (UserPatrons userPatrons : cloneEnemyPatronsList) {
			userPatrons.setCurrentBlood(userPatrons.getAbility());
			BigInteger oldBlood = userPatrons.getCurrentBlood();
			userPatrons.setCurrentBlood(new BigDecimal(oldBlood).multiply(BigDecimal.valueOf((double)userPatrons.getOreWarBattleAddition() / 1000)).
					setScale(0, BigDecimal.ROUND_UP).toBigInteger());
			userPatrons.setTotalBlood(userPatrons.getCurrentBlood());
		}
		int enemyPatronsCount = cloneEnemyPatronsList.size();

		while (true) {
			PatronsBattleDetail patronsBattleDetail;
			// 攻方先出手
			if (clonePatronsList.size() > 0 && cloneEnemyPatronsList.size() > 0) {
				patronsBattleDetail = patronsBattle(clonePatronsList.get(0), cloneEnemyPatronsList.get(0));
			} else {
				logger.error("战斗数据出错 patrons size=={},enemy patrons size=={},userId=={},enemyUserId=={}",
						clonePatronsList.size(), cloneEnemyPatronsList.size(), userId, enemyUserId);
				break;
			}

			if (clonePatronsList.get(0).getCurrentBlood().compareTo(BigInteger.ZERO) <= 0) {
				clonePatronsList.remove(0);
			}
			if (cloneEnemyPatronsList.get(0).getCurrentBlood().compareTo(BigInteger.ZERO) <= 0) {
				cloneEnemyPatronsList.remove(0);
			}
			if(null != patronsBattleDetail) {
				battleResult.getPatronsBattleDetailList().add(patronsBattleDetail);
			}

			//全部打完则退出战斗
			if (clonePatronsList.size() == 0) {//对方获胜
				battleResult.setWinUserId(enemyUserId);
				break;
			}

			if (cloneEnemyPatronsList.size() == 0) {
				battleResult.setWinUserId(userId);
				break;
			}
		}

		battleResult.setKillPatronsCount(enemyPatronsCount - cloneEnemyPatronsList.size());
		battleResult.setEnemyPatronsList(enemyPatronsList);
		battleResult.setMyPatronsIdList(patronsIdList);

		return battleResult;
	}

	private static PatronsBattleDetail patronsBattle(UserPatrons myPatrons, UserPatrons enemyPatrons) {

		PatronsBattleDetail battleDetail = new PatronsBattleDetail();

		BigInteger myDamage = myPatrons.getCurrentBlood();
		BigInteger enemyDamage = enemyPatrons.getCurrentBlood();
		enemyPatrons.setCurrentBlood(enemyPatrons.getCurrentBlood().subtract(myDamage));
		myPatrons.setCurrentBlood(myPatrons.getCurrentBlood().subtract(enemyDamage));

		battleDetail.setMyPatronsId(myPatrons.getPatronsId());
		battleDetail.setMyPatronsCurrentBlood(myPatrons.getCurrentBlood());
		battleDetail.setMyPatronsTotalBlood(myPatrons.getTotalBlood());

		battleDetail.setEnemyPatronsId(enemyPatrons.getPatronsId());
		battleDetail.setEnemyPatronsCurrentBlood(enemyPatrons.getCurrentBlood());
		battleDetail.setEnemyPatronsTotalBlood(enemyPatrons.getTotalBlood());

		return battleDetail;
	}
}
